/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2013
 */


package com.smg.community.controller;

import java.util.List;

import javacommon.base.BaseRestSpringController;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.org.rapid_framework.page.Page;

import com.smg.community.model.SmgMember;
import com.smg.community.model.SmgMemberBehavior;
import com.smg.community.model.SmgMemberFocus;
import com.smg.community.setting.Constant;
import com.smg.community.vo.query.SmgMemberBehaviorQuery;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/behavior")
public class SmgMemberBehaviorController extends BaseRestSpringController<SmgMemberBehavior,java.lang.Integer>{

	public String setBehaviorCommonModel(HttpServletRequest request ,ModelMap model,String type,int pageNumber){
		setHeadTabIndex(model, Constant.BEHAVIOR_TAB);
		setCommunityCommonModel(request, model);
		SmgMemberBehaviorQuery query = new SmgMemberBehaviorQuery();
		if ( type.equals("all")){
			if ( getLoginMember(request) == null ){
				
			}else{
				query.setMemberIdQuerySql("and memberId <>"+ getLoginMember(request).getMemberId());
			}
		}else if ( type.equals("my")){
			query.setMemberIdQuerySql("and memberId = "+ getLoginMember(request).getMemberId());
		}else if ( type.equals("friend")){
			if ( getLoginMember(request) == null ){
				return toErrorPage(model,"您未登录!");
			}
			query.setMemberIdQuerySql("and memberId  in (select focusmemberid from smg_member_focus where frommemberid = "+ getLoginMember(request).getMemberId()+")");
		}
		query.setPageNumber(pageNumber);
		setStartAndLimit(query, pageNumber,10);
		query.setSortColumns("createDate desc");
		Page page = smgMemberBehaviorManager.findPage(query);
		model.addAttribute("page",page);
		model.addAttribute("type",type);
		model.addAttribute("pageNumber",pageNumber);
		//获取的活跃大人
		SmgMember starMember = smgMemberManager.getStarMemberInOneWeek(); 
		SmgMember loginMember = getLoginMember(request);
		if ( loginMember != null){
			SmgMemberFocus smgMemberFocus = smgMemberFocusManager.getByMemberIdAndFocuseId(loginMember.getMemberId(),starMember.getMemberId());
			if ( smgMemberFocus!=null ){
				starMember.setFocusDate(smgMemberFocus.getFocusDate());
			}
		}
		
		model.addAttribute("starMember",starMember);
		query.setLimit(4);
		query.setMemberIdQuerySql("and memberId="+starMember.getMemberId());
		Page<SmgMemberBehavior> startMemberBehaviorPage = smgMemberBehaviorManager.findPage(query);
		model.addAttribute("startMemberBehaviorPage",startMemberBehaviorPage);
		List<SmgMember> memberList = smgMemberManager.getFourStaffMember();
		if (loginMember != null) {
		for (SmgMember member:memberList){
			SmgMemberFocus smgMemberFocus = smgMemberFocusManager.getByMemberIdAndFocuseId(loginMember.getMemberId(),member.getMemberId());
			if ( smgMemberFocus!=null ){
				member.setFocusDate(smgMemberFocus.getFocusDate());
			}
		}
		}
		model.addAttribute("memberList",memberList);
		return "/behavior/behaviorlist";
	}
	@RequestMapping("")
	public String listBehaviour(HttpServletRequest request,ModelMap model){
		return   listAllBehaviour(request,model,1);
	}
	
	@RequestMapping("/friend/{pageNumber}")
	public String listFriendBehaviour(HttpServletRequest request,ModelMap model,@PathVariable int pageNumber){
		return setBehaviorCommonModel(request,model,"friend",pageNumber);
	}
	
	@RequestMapping("/all/{pageNumber}")
	public String listAllBehaviour(HttpServletRequest request,ModelMap model,@PathVariable int pageNumber){
		return setBehaviorCommonModel(request,model,"all",pageNumber);
	}
	
	@RequestMapping("/my/{pageNumber}")
	public String listMyBehaviour(HttpServletRequest request,ModelMap model,@PathVariable int pageNumber){
		return setBehaviorCommonModel(request,model,"my",pageNumber);
	}
	
	
}

